import 'package:flutter/material.dart';
import 'package:flutter_tech_shop/src/widgets/setting_view/setting_item_model.dart';

/// 设置信息model
class SettingInfoModel extends SettingItemModel {
  final String title;
  final String value;

  SettingInfoModel({
    required this.title,
    required this.value,
  }) : super(
    title: title,
    value: value,
    type: SettingItemType.info,
  );
}

/// 分割组件model
class SettingDividerModel extends SettingItemModel {
  SettingDividerModel()
      : super(
    title: '',
    value: '',
    type: SettingItemType.divider,
  );
}

enum SettingItemType {
  info,
  divider,
}

/// 设置信息展示面板
class SettingView extends StatelessWidget {
  /// 数据源
  final List<SettingItemModel> data;

  /// 构建item
  final Function(BuildContext context, int index)? itemBuilder;

  /// 构建分割组件
  final Function(BuildContext context, int index)? dividerBuilder;

  /// 点击回调
  final Function(SettingItemModel settingItem)? itemOnTap;

  const SettingView({
    super.key,
    required this.data,
    this.itemBuilder,
    this.dividerBuilder,
    this.itemOnTap,
  });

  @override
  Widget build(BuildContext context) {

    defaultItemBuilder(context, index) {
      return Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        height: 48,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(data[index].title ?? 'title'),
            SizedBox(width: 10),
            Expanded(child: Text(data[index].value ?? 'value')),
          ],
        ),
      );
    }

    defaultDividerBuilder(context, index) {
      return Container(
        height: 10,
        color: const Color(0xffeeeeee),
      );
    }

    /// 默认item
    final settingItemBuilder = itemBuilder ?? defaultItemBuilder;

    /// 默认分割widget
    final settingDividerBuilder = dividerBuilder ?? defaultDividerBuilder;

    return ListView.builder(
        itemBuilder: (context, index) {
          if (data[index].type == SettingItemType.divider) {
            return settingDividerBuilder(context, index);
          }
          // return settingItemBuilder(context, index);
          return Ink(
            child: InkWell(
              onTap: () {
                itemOnTap?.call(data[index]);
              },
              child: settingItemBuilder(context, index),
            ),
          );
        },
        itemCount: data.length);
  }
}